package com.yxjme.redrainlibrary.v;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yxjme.redrainlibrary.R;
import com.yxjme.redrainlibrary.a.CountDownUtil;
import com.yxjme.redrainlibrary.i.AddCountDownListener;
import com.yxjme.redrainlibrary.m.RedPacket;

import java.util.ArrayList;
import java.util.List;

/**
 * 展示红包雨
 */
public class ShowRedRainView extends RelativeLayout {

    private LaunchRedPacketView launchRedPacketView;
    /*下落时间*/
    private int duration = 4000;
    /*增强点击范围*/
    private float addTouch = 30;
    /*字体颜色*/
    private int textColor = Color.BLACK;
    private TextView tvShowRedVal;
    /*当前需要下放的红包*/
    private List<Object> AllRedPacket = new ArrayList<>();
    /*当前选中的红包*/
    private List<RedPacket> checkRedPacket = new ArrayList<>();
    /*倒计时时间  默认30秒*/
    private int countDownTime = 30;
    /*中红包数量*/
    private int allRedPacketNum = 0;
    /*每个红包发射间隔时间*/
    private int tempTime;
    /*调整倒计时完成金币下落的位置*/
    private int redPacketDownSeep = 10;
    /*倒计时*/
    private CountDownUtil countDownUtil;
    private AddCountDownListener addCountDownListener;


    public ShowRedRainView(Context context) {
        this(context, null);
    }

    public ShowRedRainView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShowRedRainView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttr(attrs, defStyleAttr);
        initView();
    }


    public ShowRedRainView setRedPacketDownSeep(int redPacketDownSeep) {
        this.redPacketDownSeep = redPacketDownSeep;
        return ShowRedRainView.this;
    }


    /**
     * 金币倒计时 时间
     *
     * @param countDownTime
     */
    public ShowRedRainView setCountDownTime(int countDownTime) {
        this.countDownTime = countDownTime;
        return ShowRedRainView.this;
    }


    /**
     * 金币大小
     */
    public ShowRedRainView setRedPacketSize(float r) {
        launchRedPacketView.setRedPacketSize(r);
        return ShowRedRainView.this;
    }


    /**
     * 属性
     *
     * @param attrs
     * @param defStyleAttr
     */
    private void initAttr(AttributeSet attrs, int defStyleAttr) {
        TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.ShowRedRainView, 0, defStyleAttr);
        for (int i = 0; i < array.getIndexCount(); i++) {
            int attr = array.getIndex(i);
            if (attr == R.styleable.ShowRedRainView_duration) {
//                duration = array.getInt(duration, 0);
            } else if (attr == R.styleable.ShowRedRainView_addTouch) {
                addTouch = dpToPx(array.getInt(attr, 0));
            } else if (attr == R.styleable.ShowRedRainView_textColor) {
                textColor = array.getColor(attr, Color.YELLOW);
            }
        }
        array.recycle();
    }


    /**
     * @param duration
     */
    public ShowRedRainView setDuration(int duration) {
        launchRedPacketView.setDuration(duration);
        return ShowRedRainView.this;
    }

    /**
     * @param dp
     * @return
     */
    public int dpToPx(int dp) {
        float f = getResources().getDisplayMetrics().density;
        return (int) (f * dp + 0.5f);
    }



    /**
     * 活动金币雨样色
     * @param drawables
     */
    public void setDrawables(int[] drawables) {
        if (drawables!=null && launchRedPacketView!=null){
            launchRedPacketView.setDrawables(drawables);
        }
    }



    private void initView() {
        launchRedPacketView = new LaunchRedPacketView(getContext());
        launchRedPacketView.setOnAddClickListener(onAddClickListener);
        if (duration != 0) {
            launchRedPacketView.setDuration(duration);
        }
        if (addTouch != 0) {
            launchRedPacketView.setAddTouch((int) addTouch);
        }
        addView(launchRedPacketView, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        tvShowRedVal = new TextView(getContext());
        tvShowRedVal.setTextColor(textColor);
        tvShowRedVal.setTextSize(50);
        addView(tvShowRedVal);
    }


    /**
     * @param list
     * @param <T>
     */
    public <T> void setData(List<T> list) {
        if (list != null && list.size() > 0) {
            AllRedPacket.addAll(list);
            allRedPacketNum = list.size();
            /*计算每一个发射的间隔时间*/
            tempTime = (countDownTime * 1000) / allRedPacketNum;
        }
    }


    /**
     * 开始下放红包
     */
    public void start() {
        if (allRedPacketNum == 0) {
            Toast.makeText(getContext(), "红包数为零,无法开始！", Toast.LENGTH_LONG).show();
            return;
        }
        stop();
        countDownUtil = new CountDownUtil();
        countDownUtil.setTimeSeconds(countDownTime)
                .setSpeed(tempTime - redPacketDownSeep)
                .addCountDownListener(addCountDownListener = new MyAddCountDownListener())
                .startCountDown();
    }


    public class MyAddCountDownListener implements AddCountDownListener {

        @Override
        public void onCountDowning(CountDownUtil.TimeBean timeBean) {
            launchRedPacketView.lunchCount(getRedPacket());
            if (onCurrentRedPacketListener != null) {
                onCurrentRedPacketListener.onCountTime(timeBean);
            }
        }

        @Override
        public void onCountComplete() {
            if (onCurrentRedPacketListener != null)
                onCurrentRedPacketListener.onComplete();
        }

        @Override
        public void onError(String error) {

        }

        @Override
        public void onReset() {

        }
    }


    /**
     * 重置
     */
    public void reset() {
        if (countDownUtil != null) {
            countDownUtil.reset();
            countDownUtil = null;
            addCountDownListener = null;
        }
        /*移除所有金币*/
        launchRedPacketView.removeAllViews();
        /*所有数量*/
        allRedPacketNum = 0;
        /*所有金币集合*/
        AllRedPacket.clear();
        /*选中集合*/
        checkRedPacket.clear();
    }


    public void stop() {
        if (countDownUtil != null) {
            countDownUtil.reset();
            countDownUtil = null;
            addCountDownListener = null;
            /*选中集合*/
            checkRedPacket.clear();
            /*移除所有金币*/
            launchRedPacketView.removeAllViews();
        }
    }


    /**
     * 每次下放红包
     *
     * @return
     */
    private List<Object> getRedPacket() {
        if (AllRedPacket != null && AllRedPacket.size() > 0) {
            Object obj = AllRedPacket.get(0);
            List<Object> tempRedPacket = new ArrayList<>();
            tempRedPacket.add(obj);
            AllRedPacket.remove(0);
            return tempRedPacket;
        }
        return null;
    }


    /**
     * 红包点击事件回调
     */
    private LaunchRedPacketView.OnAddClickListener onAddClickListener = new LaunchRedPacketView.OnAddClickListener() {

        @Override
        public void onClick(RedPacket redPacket) {
            checkRedPacket.add(redPacket);
            if (onCurrentRedPacketListener != null) {
                onCurrentRedPacketListener.onCurrentRedPacket(redPacket, tvShowRedVal, checkRedPacket);
            }
        }


        @Override
        public void onPointXY(float x, float y) {
            if (tvShowRedVal != null) {
                tvShowRedVal.setTranslationX(x - tvShowRedVal.getMeasuredWidth() / 2);
                tvShowRedVal.setTranslationY(y - tvShowRedVal.getMeasuredHeight() / 2);
                tvShowRedVal.setAlpha(1);
                AnimatorSet enterSet = new AnimatorSet();
                enterSet.playTogether(ObjectAnimator.ofFloat(tvShowRedVal, "scaleX", 1, 0), ObjectAnimator.ofFloat(tvShowRedVal, "scaleY", 1, 0));
                enterSet.setDuration(500).start();
            }
        }
    };


    public interface OnCurrentRedPacketListener {
        void onCurrentRedPacket(RedPacket currentRedPacket, TextView textView, List<RedPacket> allRedPacket);

        void onCountTime(CountDownUtil.TimeBean timeBean);

        void onComplete();
    }

    private OnCurrentRedPacketListener onCurrentRedPacketListener;

    public void setOnAddClickListener(OnCurrentRedPacketListener onCurrentRedPacketListener) {
        this.onCurrentRedPacketListener = onCurrentRedPacketListener;
    }
}
